﻿
app.controller('SafetyOrganizationController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetOrganizationPageList = function (model, page) {
        $scope.PageLoading = true;
        $scope.TeamList = [];
        $http({
            method: 'POST',
            url: '../SafetyOrganization/GetOrganizationPageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.DataList = ret.Data.Data;

                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetOrganizationList = function () {
        $scope.PageLoading = true;
        $scope.OrgList = [];
        $http({
            method: 'POST',
            url: '../SafetyOrganization/GetOrganizationList',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.PageLoading = false;
                if (ret.Code == 100) {
                    $scope.OrgList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.AddOrgEmp = function (model,form, searchmodel, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyOrganization/AddOrgEmp',
            data: { 'model': model, 'FormId': form.Id }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    $scope.GetOrganizationPageList(searchmodel, page);
                    $("#btn_close5").click();
                }, 1000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.AddOrganization = function (model,searchmodel, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyOrganization/AddOrganization',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    $scope.GetOrganizationPageList(searchmodel, 1);
                    $("#btn_close").click();
                }, 2000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetCurrentModel = function (model) {
        var m = JSON.stringify(model);
        $scope.CurrentModel = JSON.parse(m);
        $scope.CurrentModel.CreateTime = $filter('JSONDate')($scope.CurrentModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
        console.log($scope.CurrentModel.CreateTime);
    }

    $scope.EditOrganization = function (model, searchmodel, page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../SafetyOrganization/EditOrganization',
            data: { 'model': model }
        }).success(function (ret) {
            $scope.DataLoading = false;
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                alertify.alert(ret.Msg);
                setTimeout(function () {
                    $scope.GetOrganizationPageList(searchmodel, page);
                    $("#btn_close_1").click();
                }, 2000);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            $scope.DataLoading = false;
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetAllUserList = function (control, defaultvalue) {
        //$scope.PageLoading = true;
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Construction/GetAllUserList'
        })
            .success(function (ret) {
                //common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id + ""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.refreshOption = function (control) {
        $("#" + control).selectpicker('refresh');
    }


    $scope.InitChart = function () {
 /*       let w1 = document.querySelector('.tabular')*/
        let t1 = document.querySelector('.navbar')
        let t2 = document.querySelector('.page-heading')
        let t3 = document.querySelector('.breadcrumb')
        $scope.allwidth = window.innerWidth - 260
        $scope.allheight = window.innerHeight - t1.offsetHeight - t2.offsetHeight - t3.offsetHeight -100
        console.log(t1.offsetHeight)
        console.log($scope.allwidth, $scope.allheight)

    }

    
});